package cn.com.anysdk.starter.config.oss;

import cn.com.anysdk.oss.api.IOssService;
import cn.com.anysdk.oss.config.AliyunOssConfig;
import cn.com.anysdk.oss.impl.AliyunOssService;
import com.aliyun.oss.OSS;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(AliyunOssProperties.class)
@ConditionalOnProperty(prefix = "anysdk.oss.aliyun", name = "enabled", havingValue = "true", matchIfMissing = true)
public class AliyunOssAutoConfiguration {

    @Bean
    public AliyunOssConfig aliyunOssConfig(AliyunOssProperties properties) {
        return new AliyunOssConfig.Builder()
                .endpoint(properties.getEndpoint())
                .accessKeyId(properties.getAccessKeyId())
                .accessKeySecret(properties.getAccessKeySecret())
                .bucketName(properties.getBucketName())
                .isPrivate(properties.isPrivate())
                .isIntranet(properties.isIntranet())
                .customDomain(properties.getCustomDomain())
                .build();
    }

    @Bean
    public OSS ossClient(AliyunOssConfig config) {
        return config.createOssClient();
    }

    @Bean("aliyunOssService")
    public IOssService aliyunOssService(AliyunOssConfig config) {
        return new AliyunOssService(config);
    }
}